package com.sghy.xcxservice.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sghy.xcxservice.common.CustomException;
import com.sghy.xcxservice.common.Error;
import com.sghy.xcxservice.entity.BgbClockIn;
import com.sghy.xcxservice.entity.BgbRoomContent;
import com.sghy.xcxservice.service.BgbOrgUserService;
import com.sghy.xcxservice.service.BgbRoomContentService;
import com.sghy.xcxservice.mapper.BgbRoomContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.sghy.xcxservice.common.TokenInterceptor.getUid;

/**
* @author pc
* @description 针对表【bgb_room_content(room中发布的内容)】的数据库操作Service实现
* @createDate 2025-05-30 16:11:31
*/
@Service
public class BgbRoomContentServiceImpl extends ServiceImpl<BgbRoomContentMapper, BgbRoomContent>
    implements BgbRoomContentService{
    
    @Autowired
    private BgbOrgUserService bgbOrgUserService;
    
    @Override
    public void publishDynamic(String uid, String roomNo, String content, String files) {
        BgbRoomContent bgbRoomContent = new BgbRoomContent();
        bgbRoomContent.setUId(uid);
        bgbRoomContent.setRoomNo(roomNo);
        bgbRoomContent.setContent(content);
        bgbRoomContent.setFiles(JSONArray.parseArray(files));
        this.save(bgbRoomContent);
        
    }
    
    @Override
    public void publishDynamic(String contentId, String uid, String roomNo, String content, String files) {
        BgbRoomContent bgbRoomContent = this.getById(contentId);
        if (bgbRoomContent == null) {
            throw new CustomException(Error.NO_DATA, "内容不存在");
        }
        if (!Objects.equals(bgbRoomContent.getUId(), uid)) {
            throw new CustomException(Error.NO_ROLE, "没有权限修改该内容");
        }
        bgbRoomContent.setContent(content);
        bgbRoomContent.setFiles(JSONArray.parseArray(files));
        this.updateById(bgbRoomContent);
        
    }
    
    @Override
    public List<BgbRoomContent> getProjectContentList(String roomNo, int page, int size) {
        List<BgbRoomContent> list = this.lambdaQuery()
                .eq(BgbRoomContent::getRoomNo, roomNo)
                .orderByDesc(BgbRoomContent::getTime)
                .last("limit " + (page - 1) * size + "," + size)
                .list();
        for (BgbRoomContent content : list) {
            content.setUser(bgbOrgUserService.getUserInfo(content.getUId()));
        }
        return list;
    }
    
    @Override
    public void delContent(String contentId) {
        BgbRoomContent content = this.getById(contentId);
        if (content == null) {
            throw new CustomException(Error.NO_DATA,"内容不存在");
        }
        if (!Objects.equals(content.getUId(), getUid())) {
            throw new CustomException(Error.NO_ROLE,"没有权限删除该内容");
        }
        this.removeById(contentId);
        
    }
}




